// 问诊记录工具函数
export interface ConsultationRecord {
  id: string;
  doctorId: string;
  doctorName: string;
  doctorAvatar: string;
  department: string;
  hospital: string;
  consultationType: '图文咨询' | '电话咨询' | '视频咨询';
  consultationTime: string;
  status: '咨询中' | '已完成' | '已关闭';
  lastMessage?: string;
  unreadCount: number;
  comment?: string;
}

// 添加问诊记录
export const addConsultationRecord = (doctor: any, comment?: string) => {
  const record: ConsultationRecord = {
    id: Date.now().toString(),
    doctorId: doctor.id,
    doctorName: doctor.name,
    doctorAvatar: doctor.avatar,
    department: doctor.department,
    hospital: doctor.hospital,
    consultationType: doctor.consultationMethods?.[0] || '图文咨询',
    consultationTime: new Date().toLocaleString('zh-CN'),
    status: '咨询中',
    lastMessage: '医生：您好，请问有什么症状需要咨询吗？',
    unreadCount: 1,
    comment: comment
  };

  // 从localStorage获取现有记录
  const existingRecords = localStorage.getItem('consultationRecords');
  const records = existingRecords ? JSON.parse(existingRecords) : [];
  
  // 检查是否已存在该医生的记录
  const existingIndex = records.findIndex((r: ConsultationRecord) => r.doctorId === doctor.id);
  
  if (existingIndex >= 0) {
    // 更新现有记录
    records[existingIndex] = { ...records[existingIndex], ...record };
  } else {
    // 添加新记录
    records.unshift(record);
  }
  
  // 保存到localStorage
  localStorage.setItem('consultationRecords', JSON.stringify(records));
  
  return record;
};

// 获取问诊记录
export const getConsultationRecords = (): ConsultationRecord[] => {
  const records = localStorage.getItem('consultationRecords');
  return records ? JSON.parse(records) : [];
};

// 更新问诊记录状态
export const updateConsultationStatus = (doctorId: string, status: '咨询中' | '已完成' | '已关闭') => {
  const records = getConsultationRecords();
  const updatedRecords = records.map(record => 
    record.doctorId === doctorId ? { ...record, status } : record
  );
  localStorage.setItem('consultationRecords', JSON.stringify(updatedRecords));
}; 